<!doctype html>
<html>
    <head>
        <title>Databases</title>
    </head>
    <body>
        <?php
            //Create connection
            $con = mysqli_connect("127.0.0.1", "root", "");
            //Verify if there are any errors connecting to MySQL
            if(mysqli_connect_errno($con))
            {
                echo "Error connecting to MySQL: " . mysqli_connect_error();
            }
            
            //Create database
            $sql = "CREATE DATABASE test1";
            //Execute query
            if(mysqli_query($con, $sql))
            {
                echo "Database created successfully" . "<br/>";
            }
            else
            {
                echo "Error creating database: " . mysqli_error($con) . "<br/>";
            }
            mysqli_close($con);
            
            $con = mysqli_connect("127.0.0.1", "root", "", "test1");
            if(mysqli_connect_errno($con))
            {
                echo "Error in connecting to MySQL: " . mysqli_connect_error() . "<br/>";
            }
            //Create table
            $sql = "CREATE TABLE persons(ID INT NOT NULL AUTO_INCREMENT, 
                                            PRIMARY KEY(ID), 
                                            Firstname CHAR(15), 
                                            Lastname CHAR(15), 
                                            Age INT)";
            //Execute query
            if(mysqli_query($con, $sql))
            {
                echo "Table created successfully<br/>";
            }
            else
            {
                echo "Error in creating table: " . mysqli_error($con) . "<br/>";
            }
            
            //Insert data from input fields into database
            if(isset($_POST['submit']) && !empty($_POST['submit']))
            {
                $sql = "INSERT INTO persons(Firstname,Lastname,Age) 
                        VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
                if(mysqli_multi_query($con, $sql))
                {
                    echo "Values added successfully";
                }
                else
                {
                    echo "Error in adding values to table: " . mysqli_error($con);
                }
            }
            
            //Select from database
            $sql = "SELECT * from persons";
            $resut = mysqli_query($con, $sql);
            echo "<table border='1'>" . 
                    "<tr>" . 
                        "<th> First Name </th>" . 
                        "<th> Last Name </th>" . 
                    "</tr>";
            while ($row = mysqli_fetch_array($resut)) 
            {
                echo "<tr>" . 
                        "<td>" . $row['Firstname'] . "</td>" . 
                        "<td>" . $row['Lastname'] . "</td>" . 
                     "</tr>";
            }
            echo "</table>";
            
        ?>
        <form action="" method="post">
            <input type="text" name="firstname" placeholder="First Name" />
            <input type="text" name="lastname" placeholder="Last Name" />
            <input type="text" name="age" placeholder="Age" />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>
